package com.zr.task.utils;

import android.content.Context;
import android.speech.tts.TextToSpeech;

import com.zh.baselibrary.utils.LogUtils;

import java.util.Locale;

public class TextToSpeechUtils {
    private static TextToSpeechUtils textToSpeechUtils;
   private static TextToSpeech textToSpeech;

   public static  TextToSpeechUtils getInstance(Context context){
       if(textToSpeechUtils == null){
           textToSpeechUtils = new TextToSpeechUtils();
           textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
               @Override
               public void onInit(int status) {
                   if (status == TextToSpeech.SUCCESS) {
                       int result2 = textToSpeech.setLanguage(Locale.SIMPLIFIED_CHINESE);
                       boolean b = (result2 == TextToSpeech.LANG_MISSING_DATA || result2 == TextToSpeech.LANG_NOT_SUPPORTED);
                       LogUtils.e("支持："+b);
                   }else {
                       LogUtils.e("初始化失败");
                   }

               }
           });
       }
       return  textToSpeechUtils;
   }
    private void startAuto(String data) {
        // 设置音调，值越大声音越尖（女生），值越小则变成男声,1.0是常规
        textToSpeech.setPitch(1.0f);
        // 设置语速
        textToSpeech.setSpeechRate(0.3f);
        textToSpeech.speak(data,//输入中文，若不支持的设备则不会读出来
                TextToSpeech.QUEUE_FLUSH, null);

    }
    private void onStop() {
        textToSpeech.stop(); // 不管是否正在朗读TTS都被打断
        textToSpeech.shutdown(); // 关闭，释放资源
    }
}
